package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExtHelper;
import com.ruoyi.common.utils.uuid.IdGeneratorSnowflake;
import com.ruoyi.system.domain.TrainingMedicalRecord;
import com.ruoyi.system.domain.TrainingMedicalRecordCount;
import com.ruoyi.system.mapper.TrainingMedicalRecordMapper;
import com.ruoyi.system.service.ITrainingMedicalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 临床医案Service业务层处理
 *
 * @author dp
 * @date 2023-11-27
 */
@Service
public class TrainingMedicalRecordServiceImpl implements ITrainingMedicalRecordService {
    @Autowired
    private TrainingMedicalRecordMapper trainingMedicalRecordMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;

    /**
     * 查询临床医案
     *
     * @param medicalRecordId 临床医案主键
     * @return 临床医案
     */
    @Override
    public TrainingMedicalRecord selectTrainingMedicalRecordByMedicalRecordId(Long medicalRecordId) {
        return trainingMedicalRecordMapper.selectTrainingMedicalRecordByMedicalRecordId(medicalRecordId);
    }

    /**
     * 查询临床医案列表
     *
     * @param trainingMedicalRecord 临床医案
     * @return 临床医案
     */
    @Override
    public List<TrainingMedicalRecord> selectTrainingMedicalRecordList(TrainingMedicalRecord trainingMedicalRecord) {
        return trainingMedicalRecordMapper.selectTrainingMedicalRecordList(trainingMedicalRecord);
    }

    /**
     * 查询临床医案合计
     * @param submitBegin
     * @param submitEnd
     * @param baseId
     * @param RecordStatus
     * @return
     */
    @Override
    public List<TrainingMedicalRecordCount> selectTrainingMedicalRecordCount(
            String submitBegin, String submitEnd, String baseId, String RecordStatus, Long cadetId, String cadetName) {
        return trainingMedicalRecordMapper.selectTrainingMedicalRecordCount(
                submitBegin, submitEnd, baseId, RecordStatus, cadetId, cadetName);
    }

    /**
     * 根据条件查询医案列表
     * @param submitBegin
     * @param submitEnd
     * @param baseId
     * @param cadetId
     * @param RecordStatus
     * @return
     */
    @Override
    public List<TrainingMedicalRecord> getRecordList(String submitBegin, String submitEnd, String baseId, String cadetId, String RecordStatus) {
        return trainingMedicalRecordMapper.getRecordList(submitBegin, submitEnd, baseId, cadetId, RecordStatus);
    }

    /**
     * 新增临床医案
     *
     * @param trainingMedicalRecord 临床医案
     * @return 结果
     */
    @Override
    public int insertTrainingMedicalRecord(TrainingMedicalRecord trainingMedicalRecord) {
        trainingMedicalRecord.setCreateTime(DateUtils.getNowDate());
        if (trainingMedicalRecord.getMentorId() != null && trainingMedicalRecord.getMentorId().isEmpty()) {
            trainingMedicalRecord.setMentorId(null);
        }
        return trainingMedicalRecordMapper.insertTrainingMedicalRecord(trainingMedicalRecord);
    }

    /**
     * 修改临床医案
     *
     * @param trainingMedicalRecord 临床医案
     * @return 结果
     */
    @Override
    public int updateTrainingMedicalRecord(TrainingMedicalRecord trainingMedicalRecord) {
        if (trainingMedicalRecord.getMentorId() != null && trainingMedicalRecord.getMentorId().isEmpty()) {
            trainingMedicalRecord.setMentorId(null);
        }
        return trainingMedicalRecordMapper.updateTrainingMedicalRecord(trainingMedicalRecord);
    }

    /**
     * 批量删除临床医案
     *
     * @param medicalRecordIds 需要删除的临床医案主键
     * @return 结果
     */
    @Override
    public int deleteTrainingMedicalRecordByMedicalRecordIds(Long[] medicalRecordIds) {
        return trainingMedicalRecordMapper.deleteTrainingMedicalRecordByMedicalRecordIds(medicalRecordIds);
    }

    /**
     * 删除临床医案信息
     *
     * @param medicalRecordId 临床医案主键
     * @return 结果
     */
    @Override
    public int deleteTrainingMedicalRecordByMedicalRecordId(Long medicalRecordId) {
        return trainingMedicalRecordMapper.deleteTrainingMedicalRecordByMedicalRecordId(medicalRecordId);
    }

    /**
     * 实体数据补全
     *
     * @param trainingMedicalRecord 实体
     * @param userId                用户id
     * @param crateType             创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(TrainingMedicalRecord trainingMedicalRecord, Long userId, Integer crateType) {
        //主键
        if (trainingMedicalRecord.getMedicalRecordId() == null || "".equals(trainingMedicalRecord.getMedicalRecordId())) {
            trainingMedicalRecord.setMedicalRecordId(ExtHelper.toString(idGeneratorSnowflake.nextId()));
        }
        //创建人、创建时间
        if (crateType == 1) {
            if (trainingMedicalRecord.getCreateBy() == null) {
                trainingMedicalRecord.setCreateBy(userId.toString());
            }
            if (trainingMedicalRecord.getCreateTime() == null) {
                trainingMedicalRecord.setCreateTime(DateUtils.getNowDate());
            }
        }
        //修改人
        if (trainingMedicalRecord.getModifyBy() == null) {
            trainingMedicalRecord.setModifyBy(userId);
        }
        //修改时间
        if (trainingMedicalRecord.getModifyTime() == null) {
            trainingMedicalRecord.setModifyTime(DateUtils.getNowDate());
        }
    }
}
